{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## _*Gate Errors*_ \n",
    "\n",
    "\n",
    "***\n",
    "### Contributors\n",
    "David McKay"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "The general form of an arbitrary single qubit gate is a rotation $\\theta$ around some axis in the Bloch sphere with unit vector $\\hat{n}$ such that\n",
    "$$U(\\theta,\\hat{n}) = e^{-i \\theta \\hat{n} \\cdot \\sigma} $$\n",
    "Physically these rotations are performed by applying some oscillating electromagnetic field at the qubit frequency to the location of the qubit. It is not necessary to calibrate all such rotations, only a few rotation angles and axes are enough. At minimum we need a $\\pi/2$ rotation ($\\theta=\\pi/4$) along two orthogonal axis. So then the calibration question becomes how to tell that the gate $X90p$ ($\\pi/2$ around $X$) is really $ e^{-i \\frac{\\pi}{4} \\sigma_X} $?\n",
    "\n",
    "Here we will look at different sequences to amplify possible errors in the gate.\n",
    "\n",
    "\n",
    "**Contents**\n",
    "\n",
    "[Amplitude](#sect1)\n",
    "\n",
    "[Phase](#sect2)\n",
    "\n",
    "\n",
    "### References\n",
    "\n",
    "[1]<a id=\"ref1\"></a> David C. McKay, Christopher J. Wood, Sarah Sheldon, Jerry M. Chow and Jay M. Gambetta. Efficient Z-Gates for Quantum Computing. https://arxiv.org/abs/1612.00858"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Code imports\n",
    "=============="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-18T15:13:54.475305Z",
     "start_time": "2018-12-18T15:13:53.026353Z"
    }
   },
   "outputs": [],
   "source": [
    "# Import general libraries (needed for functions)\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "# Import Qiskit classes\n",
    "import qiskit \n",
    "from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, Aer\n",
    "from qiskit.providers.aer import noise\n",
    "from qiskit.tools.visualization import plot_histogram\n",
    "\n",
    "# Import measurement calibration functions\n",
    "from qiskit.ignis.mitigation.measurement import (complete_meas_cal,\n",
    "                                                       CompleteMeasFitter, MeasurementFilter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='sect1'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Amplitude Error\n",
    "\n",
    "Consider a gate which is a X90p, but with a slight rotation error. \n",
    "$$U = e^{-i (\\pi/2+d\\theta) \\sigma_X/2}  $$\n",
    "One way to measure $d\\theta$ would be to apply the gate starting with the qubit in the ground state and look at the $|0\\rangle$ and $|1\\rangle$ counts. However, the signal could be lost in the noise. To amplify the roration error we continually apply the gate in groups of 2,\n",
    "$$U = X90p-[X90p-X90p]^n$$\n",
    "\n",
    "To predict the outcome we expand using Euler's formula,\n",
    "$$X90p = \\text{cos}\\left(\\frac{\\pi}{4}+\\frac{d\\theta}{2}\\right)I - \\text{sin}\\left(\\frac{\\pi}{4}+\\frac{d\\theta}{2}\\right) \\sigma_X $$\n",
    "$$X90p^{2n+1} = \\text{cos}\\left(n\\frac{\\pi}{2}+nd\\theta+\\frac{\\pi}{4}+\\frac{d\\theta}{2}\\right)I - \\text{sin}\\left(n\\frac{\\pi}{2}+nd\\theta+\\frac{\\pi}{4}+\\frac{d\\theta}{2}\\right) \\sigma_X $$\n",
    "and the population in the excited state is\n",
    "$$ P_{|1\\rangle, 2n+1} = \\text{sin}^2\\left(n\\frac{\\pi}{2}+nd\\theta+\\frac{\\pi}{4}+\\frac{d\\theta}{2}\\right) $$ \n",
    "$$ P_{|1\\rangle, 2n+1} = \\frac{1}{2}-\\frac{1}{2}\\text{cos}\\left(n\\pi+2nd\\theta+\\frac{\\pi}{2}+d\\theta\\right) $$ \n",
    "$$ P_{|1\\rangle, 2n+1} = \\frac{1}{2}+\\frac{(-1)^n}{2}\\text{sin}\\left(2nd\\theta+d\\theta\\right) $$ \n",
    "for small $d\\theta$ this amplifies the error by $n$,\n",
    "$$ P_{|1\\rangle, 2n+1} \\approx \\frac{1}{2}+\\frac{(-1)^n d\\theta}{2}(2n+1) $$ \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='sect2'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Phase Error\n",
    "\n",
    "A phase error means that the angle between the X90p and Y90p is not perfectly $\\pi/2$. We assume the amplitude is correct so,\n",
    "$$X90p = e^{-i \\frac{\\pi}{4}\\sigma_X}$$\n",
    "$$Y90p = e^{-i \\frac{\\pi}{4}\\left[\\text{cos}(d\\phi)\\sigma_Y+\\text{sin}(d\\phi)\\sigma_X\\right]}$$\n",
    "The angle error can be amplified by the sequence\n",
    "$$X90p-[\\{Y90p\\}^2 \\{X90p\\}^2]^n - Y90p$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Tags",
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
